<template>
  <div class="user-points-manage-page">
    <h1>用户积分管理</h1>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="用户积分查询" name="query">
        <section class="points-section">
          <h2>用户积分查询</h2>
          <div class="filter-bar">
            <el-input v-model="searchForm.keyword" placeholder="手机号/昵称/用户ID" style="width: 220px; margin-right: 12px;" clearable />
            <el-select v-model="searchForm.level" placeholder="等级" style="width: 120px; margin-right: 12px;">
              <el-option label="全部" value="" />
              <el-option label="L1" value="L1" />
              <el-option label="L2" value="L2" />
              <el-option label="L3" value="L3" />
              <el-option label="L4" value="L4" />
              <el-option label="L5" value="L5" />
              <el-option label="L6" value="L6" />
              <el-option label="L7" value="L7" />
            </el-select>
            <el-date-picker v-model="searchForm.registerRange" type="daterange" range-separator="至" start-placeholder="注册开始" end-placeholder="注册结束" style="width: 240px; margin-right: 12px;" />

            <el-button type="primary" @click="searchUser">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </div>
          <el-table :data="userList" style="margin-top: 16px">
            <el-table-column prop="userId" label="用户ID" width="120" />
            <el-table-column prop="nickname" label="昵称" width="120" />
            <el-table-column prop="phone" label="手机号" width="140" />
            <el-table-column prop="points" label="积分余额" width="100" />
            <el-table-column prop="level" label="等级" width="100" />

            <el-table-column label="操作" width="260">
              <template #default="scope">
                <el-button size="small" @click="viewDetail(scope.row)">明细</el-button>
                <el-button size="small" @click="openAdjustDialog(scope.row)">调整积分</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            v-model:current-page="pagination.current"
            v-model:page-size="pagination.size"
            :total="pagination.total"
            layout="total, sizes, prev, pager, next, jumper"
            :page-sizes="[10, 20, 50, 100]"
            style="margin-top: 16px; text-align: right;"
          />
        </section>
      </el-tab-pane>
      <el-tab-pane label="待审批" name="approval">
        <section class="points-section">
          <h2>积分调整待审批</h2>
          <div class="filter-bar">
            <el-select v-model="approvalFilter.status" placeholder="审批状态" style="width: 120px; margin-right: 12px;">
              <el-option label="全部" value="" />
              <el-option label="待审批" value="pending" />
              <el-option label="已通过" value="approved" />
              <el-option label="已拒绝" value="rejected" />
            </el-select>
            <el-input v-model="approvalFilter.operator" placeholder="操作员" style="width: 140px; margin-right: 12px;" clearable />
            <el-button type="primary" @click="searchApproval">查询</el-button>
            <el-button @click="resetApprovalFilter">重置</el-button>
          </div>
          <el-table :data="filteredApprovalList" style="margin-top: 16px">
            <el-table-column prop="operationType" label="操作类型" width="100">
              <template #default="scope">
                <el-tag :type="scope.row.operationType === 'single' ? 'info' : 'warning'">
                  {{ scope.row.operationType === 'single' ? '单个调整' : '批量调整' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="user" label="用户" width="120">
              <template #default="scope">
                <span v-if="scope.row.operationType === 'single'">{{ scope.row.user }}</span>
                <div v-else>
                  <el-button 
                    type="primary" 
                    size="small" 
                    text 
                    @click="viewBatchUsers(scope.row)"
                  >
                    {{ scope.row.users?.length || 0 }}个用户
                  </el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="调整类型" width="100">
              <template #default="scope">
                <el-tag :type="scope.row.type === 'add' ? 'success' : 'danger'">
                  {{ scope.row.type === 'add' ? '增加' : '减少' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="amount" label="积分数量" width="100" />
            <el-table-column prop="reason" label="调整原因" />
            <el-table-column prop="operator" label="操作员" width="100" />
            <el-table-column prop="applyTime" label="申请时间" width="160" />
            <el-table-column prop="status" label="审批状态" width="100">
              <template #default="scope">
                <el-tag :type="scope.row.status === 'pending' ? 'warning' : (scope.row.status === 'approved' ? 'success' : 'info')">
                  {{ scope.row.status === 'pending' ? '待审批' : (scope.row.status === 'approved' ? '已通过' : '已拒绝') }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button size="small" type="primary" v-if="scope.row.status === 'pending' && currentUser.role === 'leader'" @click="openApprovalDialog(scope.row)">审批</el-button>
                <el-button size="small" v-if="scope.row.status !== 'pending'" @click="viewApprovalDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </section>
      </el-tab-pane>
    </el-tabs>

    <el-dialog v-model="adjustDialogVisible" title="调整积分" width="400px" @close="resetAdjustForm">
      <el-form :model="adjustForm" label-width="80px">
        <el-form-item label="调整类型">
          <el-radio-group v-model="adjustForm.type">
            <el-radio value="add">增加积分</el-radio>
            <el-radio value="subtract">减少积分</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="调整数量">
          <el-input-number v-model="adjustForm.amount" :min="1" />
        </el-form-item>
        <el-form-item label="调整原因">
          <el-input v-model="adjustForm.reason" type="textarea" :rows="3" placeholder="请输入调整原因" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="adjustDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmAdjust">确定调整</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="approvalDialogVisible" :title="approvalDialogTitle" width="500px" @close="resetApprovalForm">
      <div v-if="approvalCurrent">
        <el-descriptions :column="1" border style="margin-bottom: 16px;">
          <el-descriptions-item label="操作类型">
            <el-tag :type="approvalCurrent.operationType === 'single' ? 'info' : 'warning'">
              {{ approvalCurrent.operationType === 'single' ? '单个调整' : '批量调整' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="用户">
            <span v-if="approvalCurrent.operationType === 'single'">{{ approvalCurrent.user }}</span>
            <div v-else>
              <span>{{ approvalCurrent.users?.length || 0 }}个用户</span>
              <el-button 
                type="primary" 
                size="small" 
                text 
                @click="viewBatchUsers(approvalCurrent)"
                style="margin-left: 8px;"
              >
                查看用户列表
              </el-button>
            </div>
          </el-descriptions-item>
          <el-descriptions-item label="调整类型">
            <el-tag :type="approvalCurrent.type === 'add' ? 'success' : 'danger'">
              {{ approvalCurrent.type === 'add' ? '增加' : '减少' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="积分数量">{{ approvalCurrent.amount }}</el-descriptions-item>
          <el-descriptions-item label="调整原因">{{ approvalCurrent.reason }}</el-descriptions-item>
          <el-descriptions-item label="操作员">{{ approvalCurrent.operator }}</el-descriptions-item>
          <el-descriptions-item label="申请时间">{{ approvalCurrent.applyTime }}</el-descriptions-item>
        </el-descriptions>
      </div>
      
      <el-form :model="approvalForm" label-width="80px">
        <el-form-item label="审批操作">
          <el-radio-group v-model="approvalForm.action">
            <el-radio value="approved">通过</el-radio>
            <el-radio value="rejected">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见" v-if="approvalForm.action === 'rejected'">
          <el-input v-model="approvalForm.reason" type="textarea" :rows="3" placeholder="请输入拒绝原因" />
        </el-form-item>
        <el-form-item label="审批意见" v-else>
          <el-input v-model="approvalForm.reason" type="textarea" :rows="3" placeholder="请输入审批意见（可选）" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="approvalDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmApproval">确定</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="approvalDetailVisible" title="审批详情" width="500px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="操作类型">
          <el-tag :type="approvalDetail.operationType === 'single' ? 'info' : 'warning'">
            {{ approvalDetail.operationType === 'single' ? '单个调整' : '批量调整' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="用户">
          <span v-if="approvalDetail.operationType === 'single'">{{ approvalDetail.user }}</span>
          <div v-else>
            <span>{{ approvalDetail.users?.length || 0 }}个用户</span>
            <el-button 
              type="primary" 
              size="small" 
              text 
              @click="viewBatchUsers(approvalDetail)"
              style="margin-left: 8px;"
            >
              查看用户列表
            </el-button>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="调整类型">
          <el-tag :type="approvalDetail.type === 'add' ? 'success' : 'danger'">
            {{ approvalDetail.type === 'add' ? '增加' : '减少' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="积分数量">{{ approvalDetail.amount }}</el-descriptions-item>
        <el-descriptions-item label="调整原因">{{ approvalDetail.reason }}</el-descriptions-item>
        <el-descriptions-item label="操作员">{{ approvalDetail.operator }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ approvalDetail.applyTime }}</el-descriptions-item>
        <el-descriptions-item label="审批状态">
          <el-tag :type="approvalDetail.status === 'pending' ? 'warning' : (approvalDetail.status === 'approved' ? 'success' : 'info')">
            {{ approvalDetail.status === 'pending' ? '待审批' : (approvalDetail.status === 'approved' ? '已通过' : '已拒绝') }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="审批人" v-if="approvalDetail.approver">{{ approvalDetail.approver }}</el-descriptions-item>
        <el-descriptions-item label="审批意见" v-if="approvalDetail.approvalComment">{{ approvalDetail.approvalComment }}</el-descriptions-item>
        <el-descriptions-item label="审批时间" v-if="approvalDetail.approvalTime">{{ approvalDetail.approvalTime }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="approvalDetailVisible = false">关闭</el-button>
      </template>
    </el-dialog>

    <!-- 批量用户查看弹窗 -->
    <el-dialog v-model="batchUsersVisible" title="批量用户列表" width="600px">
      <div style="max-height: 400px; overflow-y: auto;">
        <el-tag 
          v-for="user in currentBatchUsers" 
          :key="user" 
          size="large" 
          style="margin: 4px;"
        >
          {{ user }}
        </el-tag>
      </div>
      <template #footer>
        <el-button @click="batchUsersVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

interface UserItem {
  userId: string
  nickname: string
  phone: string
  points: number
  level: string
  registerTime: string
}

const searchForm = ref({
  keyword: '',
  level: '',
  registerRange: []
})

const allUserList = ref<UserItem[]>([
  { userId: '10001', nickname: '小明', phone: '13800001111', points: 1200, level: 'L2', registerTime: '2023-12-01' },
  { userId: '10002', nickname: '小红', phone: '13900002222', points: 0, level: 'L1', registerTime: '2023-11-15' },
  { userId: '10003', nickname: '小刚', phone: '13700003333', points: 350, level: 'L1', registerTime: '2023-12-10' },
  { userId: '10004', nickname: '小美', phone: '13600004444', points: 800, level: 'L3', registerTime: '2023-10-20' },
  { userId: '10005', nickname: '阿强', phone: '13800005555', points: 150, level: 'L1', registerTime: '2023-12-12' },
  { userId: '10006', nickname: '小芳', phone: '13900006666', points: 600, level: 'L2', registerTime: '2023-11-30' },
  { userId: '10007', nickname: '大壮', phone: '13700007777', points: 0, level: 'L1', registerTime: '2023-12-05' },
  { userId: '10008', nickname: '小雨', phone: '13600008888', points: 2000, level: 'L4', registerTime: '2023-10-10' },
  { userId: '10009', nickname: '小刚2', phone: '13800009999', points: 120, level: 'L1', registerTime: '2023-12-15' },
  { userId: '10010', nickname: '小美2', phone: '13900001010', points: 900, level: 'L2', registerTime: '2023-11-18' },
  { userId: '10011', nickname: '小明3', phone: '13700001111', points: 300, level: 'L1', registerTime: '2023-12-02' },
  { userId: '10012', nickname: '小红3', phone: '13600001212', points: 0, level: 'L1', registerTime: '2023-11-20' },
  { userId: '10013', nickname: '小刚3', phone: '13800001313', points: 450, level: 'L2', registerTime: '2023-12-11' },
  { userId: '10014', nickname: '小美3', phone: '13900001414', points: 700, level: 'L3', registerTime: '2023-10-22' },
  { userId: '10015', nickname: '阿强2', phone: '13700001515', points: 180, level: 'L1', registerTime: '2023-12-13' },
  { userId: '10016', nickname: '小芳2', phone: '13600001616', points: 650, level: 'L2', registerTime: '2023-11-29' },
  { userId: '10017', nickname: '大壮2', phone: '13800001717', points: 0, level: 'L1', registerTime: '2023-12-06' },
  { userId: '10018', nickname: '小雨2', phone: '13900001818', points: 2100, level: 'L5', registerTime: '2023-10-12' },
  { userId: '10019', nickname: '小刚4', phone: '13700001919', points: 130, level: 'L1', registerTime: '2023-12-16' },
  { userId: '10020', nickname: '小美4', phone: '13600002020', points: 950, level: 'L2', registerTime: '2023-11-19' },
  { userId: '10021', nickname: '小明4', phone: '13800002121', points: 320, level: 'L1', registerTime: '2023-12-03' },
  { userId: '10022', nickname: '小红4', phone: '13900002222', points: 0, level: 'L1', registerTime: '2023-11-21' },
  { userId: '10023', nickname: '小刚5', phone: '13700002323', points: 470, level: 'L2', registerTime: '2023-12-14' },
  { userId: '10024', nickname: '小美5', phone: '13600002424', points: 750, level: 'L3', registerTime: '2023-10-24' },
  { userId: '10025', nickname: '阿强3', phone: '13800002525', points: 190, level: 'L1', registerTime: '2023-12-17' },
  { userId: '10026', nickname: '小芳3', phone: '13900002626', points: 680, level: 'L2', registerTime: '2023-11-28' },
  { userId: '10027', nickname: '大壮3', phone: '13700002727', points: 0, level: 'L1', registerTime: '2023-12-07' },
  { userId: '10028', nickname: '小雨3', phone: '13600002828', points: 2200, level: 'L6', registerTime: '2023-10-14' },
  { userId: '10029', nickname: '小刚6', phone: '13800002929', points: 140, level: 'L1', registerTime: '2023-12-18' },
  { userId: '10030', nickname: '小美6', phone: '13900003030', points: 980, level: 'L2', registerTime: '2023-11-22' }
])
const userList = ref<UserItem[]>([])
const pagination = ref({
  current: 1,
  size: 10,
  total: 30
})

function updateUserListByPage(list: UserItem[]) {
  pagination.value.total = list.length
  const start = (pagination.value.current - 1) * pagination.value.size
  const end = start + pagination.value.size
  userList.value = list.slice(start, end)
}

function searchUser() {
  // mock筛选
  let list = [...allUserList.value]
  if (searchForm.value.keyword) {
    list = list.filter(u => u.userId.includes(searchForm.value.keyword) || u.nickname.includes(searchForm.value.keyword) || u.phone.includes(searchForm.value.keyword))
  }
  if (searchForm.value.level) {
    list = list.filter(u => u.level === searchForm.value.level)
  }
  if (searchForm.value.registerRange && searchForm.value.registerRange.length === 2) {
    const [start, end] = searchForm.value.registerRange
    list = list.filter(u => {
      const reg = new Date(u.registerTime).getTime()
      return (!start || reg >= new Date(start).getTime()) && (!end || reg <= new Date(end).getTime())
    })
  }
  updateUserListByPage(list)
  ElMessage.success('查询完成（mock）')
}
function resetSearch() {
  searchForm.value = { keyword: '', level: '', registerRange: [] }
  updateUserListByPage(allUserList.value)
  ElMessage.success('筛选条件已重置')
}
watch([() => pagination.value.current, () => pagination.value.size], () => {
  searchUser()
})
// 页面初始化时填充第一页
onMounted(() => {
  updateUserListByPage(allUserList.value)
  
  // 从localStorage读取批量审批数据并合并到审批列表
  const batchApprovals = JSON.parse(localStorage.getItem('batchApprovals') || '[]')
  if (batchApprovals.length > 0) {
    approvalList.value.unshift(...batchApprovals)
    // 清空localStorage中的批量审批数据
    localStorage.removeItem('batchApprovals')
  }
  
  // 检查URL参数，如果指定了tab=approval，则切换到待审批标签页
  const urlParams = new URLSearchParams(window.location.search)
  if (urlParams.get('tab') === 'approval') {
    activeTab.value = 'approval'
  }
})



const adjustDialogVisible = ref(false)
const adjustDialogTitle = ref('积分调整')
const adjustUser = ref<UserItem | null>(null)
const adjustForm = ref({
  type: 'add',
  amount: 1,
  reason: ''
})

const resetAdjustForm = () => {
  adjustForm.value = { type: 'add', amount: 1, reason: '' }
}



const actionLog = ref<{user: string, action: string, remark: string, time: string, operator: string}[]>([
  { user: '小明', action: '调整积分', remark: '手动加分', time: '2024-01-10 10:00:00', operator: '管理员' },
  { user: '10001,10003', action: '批量加减', remark: '年终奖励批量加分', time: '2024-01-13 16:00:00', operator: '系统' }
])

const currentUser = { name: '领导A', role: 'leader', canAdjust: true, canBatch: true }
const approvalFilter = ref({ status: '', operator: '' })
const filteredApprovalList = computed(() => {
  return approvalList.value.filter(item => {
    const matchStatus = !approvalFilter.value.status || item.status === approvalFilter.value.status
    const matchOperator = !approvalFilter.value.operator || item.operator.includes(approvalFilter.value.operator)
    return matchStatus && matchOperator
  })
})
function searchApproval() {
  ElMessage.success('查询完成（mock）')
}
function resetApprovalFilter() {
  approvalFilter.value = { status: '', operator: '' }
  ElMessage.success('筛选条件已重置')
}

function viewDetail(user: UserItem) {
  // 跳转到明细页面，传递用户数据
  const userData = encodeURIComponent(JSON.stringify(user))
  router.push({
    name: 'UserPointsDetail',
    params: { userId: user.userId },
    query: { userData }
  })
}

function openAdjustDialog(user: UserItem) {
  adjustUser.value = user
  adjustDialogTitle.value = `调整【${user.nickname}】积分`
  adjustForm.value = { type: 'add', amount: 1, reason: '' }
  adjustDialogVisible.value = true
}

function confirmAdjust() {
  if (!adjustUser.value) return
  if (!adjustForm.value.amount || !adjustForm.value.reason) {
    ElMessage.warning('请填写完整信息')
    return
  }
  // 不直接变动积分，写入待审批列表
  approvalList.value.unshift({
    id: Date.now(),
    operationType: 'single',
    user: adjustUser.value.nickname,
    type: adjustForm.value.type,
    amount: adjustForm.value.amount,
    reason: adjustForm.value.reason,
    operator: currentUser.name,
    applyTime: new Date().toLocaleString(),
    status: 'pending'
  })
  ElMessage.success('已提交审批，待领导审核')
  adjustDialogVisible.value = false
}





const activeTab = ref('query')
const approvalList = ref<any[]>([
  // mock数据 - 单个调整
  { 
    id: 1, 
    operationType: 'single',
    user: '小明', 
    type: 'add', 
    amount: 100, 
    reason: '手动加分', 
    operator: '操作员A', 
    applyTime: '2024-06-01 10:00:00', 
    status: 'pending' 
  },
  { 
    id: 2, 
    operationType: 'single',
    user: '小红', 
    type: 'subtract', 
    amount: 50, 
    reason: '违规扣分', 
    operator: '操作员B', 
    applyTime: '2024-06-01 11:00:00', 
    status: 'pending' 
  },
  // mock数据 - 批量调整
  { 
    id: 3, 
    operationType: 'batch',
    users: ['小刚', '小美', '阿强', '小芳'], 
    type: 'add', 
    amount: 200, 
    reason: '年终奖励批量加分', 
    operator: '系统管理员', 
    applyTime: '2024-06-01 12:00:00', 
    status: 'pending' 
  },
  // mock数据 - 大量用户批量调整
  { 
    id: 7, 
    operationType: 'batch',
    users: ['用户1', '用户2', '用户3', '用户4', '用户5', '用户6', '用户7', '用户8', '用户9', '用户10', '用户11', '用户12', '用户13', '用户14', '用户15', '用户16', '用户17', '用户18', '用户19', '用户20', '用户21', '用户22', '用户23', '用户24', '用户25', '用户26', '用户27', '用户28', '用户29'], 
    type: 'add', 
    amount: 100, 
    reason: '大规模活动奖励', 
    operator: '系统管理员', 
    applyTime: '2024-06-01 19:00:00', 
    status: 'pending' 
  },
  { 
    id: 4, 
    operationType: 'batch',
    users: ['小明', '小红', '小刚'], 
    type: 'subtract', 
    amount: 30, 
    reason: '活动违规批量扣分', 
    operator: '风控专员', 
    applyTime: '2024-06-01 13:00:00', 
    status: 'approved', 
    approver: '领导A', 
    approvalComment: '同意扣分', 
    approvalTime: '2024-06-01 14:00:00' 
  },
  { 
    id: 5, 
    operationType: 'single',
    user: '小美', 
    type: 'add', 
    amount: 150, 
    reason: '活动奖励', 
    operator: '操作员A', 
    applyTime: '2024-06-01 15:00:00', 
    status: 'approved', 
    approver: '领导A', 
    approvalComment: '同意', 
    approvalTime: '2024-06-01 16:00:00' 
  },
  { 
    id: 6, 
    operationType: 'single',
    user: '小芳', 
    type: 'subtract', 
    amount: 80, 
    reason: '异常处理', 
    operator: '操作员B', 
    applyTime: '2024-06-01 17:00:00', 
    status: 'rejected', 
    approver: '领导A', 
    approvalComment: '理由不充分', 
    approvalTime: '2024-06-01 18:00:00' 
  }
])
const approvalDialogVisible = ref(false)
const approvalDialogTitle = ref('审批积分调整')
const approvalForm = ref({ action: 'approved', reason: '' })

const resetApprovalForm = () => {
  approvalForm.value = { action: 'approved', reason: '' }
}
const approvalCurrent = ref<any>(null)
const approvalDetailVisible = ref(false)
const approvalDetail = ref<any>({})

// 批量用户查看相关
const batchUsersVisible = ref(false)
const currentBatchUsers = ref<string[]>([])

function openApprovalDialog(row: any) {
  approvalCurrent.value = row
  // 根据操作类型设置不同的标题
  if (row.operationType === 'single') {
    approvalDialogTitle.value = `审批【${row.user}】积分调整`
  } else {
    approvalDialogTitle.value = `审批【${row.users?.length || 0}个用户】积分调整`
  }
  approvalForm.value = { action: 'approved', reason: '' }
  approvalDialogVisible.value = true
}
function confirmApproval() {
  if (!approvalCurrent.value) return
  approvalCurrent.value.status = approvalForm.value.action
  approvalCurrent.value.approver = currentUser.name
  approvalCurrent.value.approvalComment = approvalForm.value.reason
  approvalCurrent.value.approvalTime = new Date().toLocaleString()
  
  if (approvalForm.value.action === 'approved') {
    // 审批通过才真正变动积分
    if (approvalCurrent.value.operationType === 'single') {
      // 单个用户调整
      const user = allUserList.value.find(u => u.nickname === approvalCurrent.value.user)
      if (user) {
        if (approvalCurrent.value.type === 'add') {
          user.points += approvalCurrent.value.amount
        } else {
          user.points = Math.max(0, user.points - approvalCurrent.value.amount)
        }
      }
    } else {
      // 批量用户调整
      approvalCurrent.value.users?.forEach((userName: string) => {
        const user = allUserList.value.find(u => u.nickname === userName)
        if (user) {
          if (approvalCurrent.value.type === 'add') {
            user.points += approvalCurrent.value.amount
          } else {
            user.points = Math.max(0, user.points - approvalCurrent.value.amount)
          }
        }
      })
    }
  }
  
  approvalDialogVisible.value = false
  ElMessage.success(`审批${approvalForm.value.action === 'approved' ? '通过' : '拒绝'}成功`)
}
function viewApprovalDetail(row: any) {
  approvalDetail.value = row
  approvalDetailVisible.value = true
}

function viewBatchUsers(row: any) {
  currentBatchUsers.value = row.users || []
  batchUsersVisible.value = true
}
</script>

<style scoped>
.user-points-manage-page {
  padding: 24px;
}
.points-section {
  margin-bottom: 32px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px #f0f1f2;
  padding: 20px;
}
.filter-bar {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}
</style> 